package com.hui.qiniucloud.service.impl;

import com.hui.qiniucloud.config.obs.OBS;
import com.hui.qiniucloud.config.obs.Uploader;
import com.hui.qiniucloud.service.ImageService;
import com.obs.services.exception.ObsException;
import com.obs.services.model.PutObjectRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Service
public class ImageImpl implements ImageService {




    public String uploadImage(MultipartFile mpFile, String type)  {
        try {
            String fileName = Uploader.getRandomName(mpFile);
            File tempFile = File.createTempFile("qiniu",fileName);
            mpFile.transferTo(tempFile);
            PutObjectRequest request = new PutObjectRequest();
            request.setFile(tempFile);
            request.setBucketName("seven-cow");
            request.setObjectKey( type + "/" + tempFile.getName());
            OBS.client.putObject(request);
            String obsUrl = "https://seven-cow.obs.cn-south-1.myhuaweicloud.com/" +type + "/" + tempFile.getName();
            return obsUrl;
        } catch (ObsException e) {
            System.out.println("putObject failed");
            // 请求失败,打印http状态码
            System.out.println("HTTP Code:" + e.getResponseCode());
            // 请求失败,打印服务端错误码
            System.out.println("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            System.out.println("Error Message:" + e.getErrorMessage());
            // 请求失败,打印请求id
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
        } catch (Exception e) {
            System.out.println("putObject failed");
            // 其他异常信息打印
            e.printStackTrace();
        }
        return null;
    }

}
